package com.project.demo.config;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class ExcelDataReader {
    public static <AttendanceRecord> List<AttendanceRecord> readAndCleanExcelData() {
        List<AttendanceRecord> records = new ArrayList<>();
        Map<String, Map<String, List<Date>>> dailyRecords = new HashMap<>();
        // 新增一个 Map 来存储每个人员每天的其他信息
        Map<String, Map<String, RecordInfo>> dailyInfo = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try (FileInputStream file = new FileInputStream(new File("C:\\Users\\27712\\Desktop\\小项目\\考勤统计报表样.xlsx"));
             XSSFWorkbook workbook = new XSSFWorkbook(file)) {

            XSSFSheet sheet = workbook.getSheet("原始考勤记录报表");
            Iterator<Row> rowIterator = sheet.iterator();

            // 跳过表头行
            if (rowIterator.hasNext()) {
                rowIterator.next();
            }

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                String id = row.getCell(0).getStringCellValue();
                String name = row.getCell(1).getStringCellValue();
                String department = row.getCell(2).getStringCellValue();
                String attendanceStatus = row.getCell(4).getStringCellValue();
                String attendancePoint = row.getCell(5).getStringCellValue();
                String customName = row.getCell(6).getStringCellValue();
                String dataSource = row.getCell(7).getStringCellValue();
                String processType = row.getCell(8).getStringCellValue();
                String temperature = row.getCell(9).getStringCellValue();
                String temperatureAbnormal = row.getCell(10).getStringCellValue();
                Cell dateCell = row.getCell(3);

//                if (dateCell.getCellType()CellType.NUMERIC) {
//                    continue;
//                }
                Date time = dateCell.getDateCellValue();
                String dateStr = dateFormat.format(time);

                dailyRecords.computeIfAbsent(name, k -> new HashMap<>())
                        .computeIfAbsent(dateStr, k -> new ArrayList<>())
                        .add(time);

                // 存储每个人员每天的其他信息
                dailyInfo.computeIfAbsent(name, k -> new HashMap<>())
                        .put(dateStr, new RecordInfo(id, department, attendanceStatus, attendancePoint, customName, dataSource, processType, temperature, temperatureAbnormal));
            }

            for (Map.Entry<String, Map<String, List<Date>>> entry : dailyRecords.entrySet()) {
                String name = entry.getKey();
                for (Map.Entry<String, List<Date>> dayEntry : entry.getValue().entrySet()) {
                    String date = dayEntry.getKey();
                    List<Date> times = dayEntry.getValue();
                    if (!times.isEmpty()) {
                        Date startTime = Collections.min(times);
                        Date endTime = Collections.max(times);

                        long durationInMillis = endTime.getTime() - startTime.getTime();
                        long hours = durationInMillis / (1000 * 60 * 60);
                        long minutes = (durationInMillis % (1000 * 60 * 60)) / (1000 * 60);
                        String attendanceDuration = hours + "小时" + minutes + "分钟";

                        // 获取存储的信息
                        RecordInfo info = dailyInfo.get(name).get(date);


                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("读取和清洗 Excel 数据失败", e);
        }

        return records;
    }

    // 新增一个内部类来存储其他信息
    static class RecordInfo {
        String id;
        String department;
        String attendanceStatus;
        String attendancePoint;
        String customName;
        String dataSource;
        String processType;
        String temperature;
        String temperatureAbnormal;

        RecordInfo(String id, String department, String attendanceStatus, String attendancePoint, String customName, String dataSource, String processType, String temperature, String temperatureAbnormal) {
            this.id = id;
            this.department = department;
            this.attendanceStatus = attendanceStatus;
            this.attendancePoint = attendancePoint;
            this.customName = customName;
            this.dataSource = dataSource;
            this.processType = processType;
            this.temperature = temperature;
            this.temperatureAbnormal = temperatureAbnormal;
        }
    }
}